#include <bits/stdc++.h>
using namespace std;
bool chCmp(char a, char b) {
    return a > b;
}
int res, len;
int n;
int getNum(char* t) {
    int ret = 0;
    for (int i=0; i<len; ++i) {
        ret = ret * 10 + t[i] - '0';
    }
    return ret;
}
void getArr(int a, char* t) {
    for (int i=0; i<len; ++i) {
        t[len-i-1] = a % 10 + '0';
        a = a / 10;
    }
}
char buf[11];
void DFS(int a, int d, int cnt, bool da) {
    if (cnt >= n || d >= len) return;
    getArr(a, buf);
    char c = buf[d];
    for (int i=len-1; i>d; --i) {
        if (da && c<buf[i]) c = buf[i];
        if (!da && c>buf[i] && d!=0) c = buf[i];
        else if (!da && c>buf[i] && d==0 && buf[i] != '0') c = buf[i];
    }
    bool flag = true;
    for (int i=d+1; i<len; ++i) {
        if (buf[i]==c && c!=buf[d]) {
            flag = false;
            swap(buf[d], buf[i]);
            int t = getNum(buf);
            if (da && res<t) res = t;
            if (!da && res>t) res = t;
            DFS(t, d+1, cnt+1, da);
            swap(buf[d], buf[i]);
        }
    }
    if (flag) {
        int t = getNum(buf);
        DFS(t, d+1, cnt, da);
    }
}


int main(int argc, char** argv) {
#ifdef hai
    freopen("1002.in", "r", stdin);
#endif
    int T;
    cin>>T;
    char myss[11];
    char maxValue[11], minValue[11];
    while(T--) {
        scanf("%s %d", myss, &n);
        len = strlen(myss);
        strcpy(maxValue, myss);
        strcpy(minValue, myss);
        if (len-1 <= n) {
            sort(minValue, minValue+len);
            if (minValue[0] == '0') {
                int t = 0;
                while(minValue[t]=='0') ++t;
                swap(minValue[0], minValue[t]);
            }
            sort(maxValue, maxValue+len, chCmp);
            printf("%s %s\n", minValue, maxValue);
        }
        else {
            int t = getNum(myss);
            res = t;
            DFS(t, 0, 0, false);
            int minV = res;
            res = t;
            DFS(t, 0, 0, true);
            int maxV = res;
            printf("%d %d\n", minV, maxV);
        }
    }
    return 0;
}
